package com.encrypt;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.Base64Utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class ThreeDESCBC {

    /**
     * @param args
     * @throws Exception
     * @Description 调试方法
     */
    public static void main(String[] args) throws Exception {
        JSONObject pams = JSONObject.parseObject(
                " {\n" +
                        "  \"shopCode\":\"1514113037508332294\",\n" +
                        "  \"systemCode\":\"TPTXXX\",\n" +
                        "  \"signature\":\"1330310edf606bad907efec0a2cf563a\",\n" +
                        "  \"organCode\":\"TPTXXX\",\n" +
                        "  \"latitude\":\"32.97054428578949\",\n" +
                        "  \"longitude\":\"112.5166368016669\",\n" +
                        "  \"timestamp\":\"1557134724536\"\n" +
                        " }"
        );

        String content = pams.toJSONString();

        DES des = new DES(Mode.CBC, Padding.PKCS5Padding, "026F298F".getBytes(), "05CBEB03".getBytes());
        byte [] encrypt = des.encrypt(content);
        System.out.println(new String(encrypt));
        System.out.println("===================");
        //解密为字符串
        String decryptStr = des.decryptStr("Y36zwEctN/kPQ6yZ1X5agMuksOim2YZwMy0VU3ds8rqzas08iCmTindyQ4OjUXG0DEXAGKawfF++NP080cY2uSTfitiTSroL");
        System.out.println(decryptStr);


    }


    /**
     * @param key  密钥
     * @param data 明文
     * @return Base64编码的密文
     * @throws Exception
     * @Description ECB加密，不要IV
     */
    public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * @param key  密钥
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     * @Description ECB解密，不要IV
     */
    public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * @param key   密钥
     * @param keyiv IV
     * @param data  明文
     * @return Base64编码的密文
     * @throws Exception
     * @Description CBC加密
     */
    public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * @param key   密钥
     * @param keyiv IV
     * @param data  Base64编码的密文
     * @return 明文
     * @throws Exception
     * @Description CBC解密
     */
    public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

}
